﻿class FindPath{
	//属性参数
	private var fps:Number;
	private var me:Object;
	private var Finder:Object;
	//构造函数
	public function FindPath(sfps:Number){
		init(sfps);
	}
	private function init(sfps:Number){
		this.fps=sfps;
		this.me=this;
	}
	private function say(o:Object){
		trace("FindPath: "+o);
	}
	//功能函数
	public function createFinder(player:MovieClip,map:MovieClip,tag:MovieClip):Object{
		var finder:MovieClip;
		var playHitMap:Object;
		var finderO:Object;
		
		finder=createFinderMc(player);   //创建寻路算法mc
		playHitMap=getHitTestMap(player,1);    //找出运动者外轮廓
	
		if(finder==null)return null;
		if(playHitMap==null){
			finder.destroy();
			return null;
		}
		//---------- finder object
		finderO.tag=finder;
		finderO.path=new Array();
		//---------- finder movieclip
		finder.playerMap=localToGlobal(player,playHitMap);
		finder.player=player;
		finder.map=map;
		finder.tag=tag;
		
		finder.fo=finderO;
		finder.times=0;
		finder.nowTimes=0;
		finder.sf=false;
		finder.sj=0.6
		finder.fps=this.fps;
		finder.startFind=function(){
			this.sf=true;
			finder.times=(new Date()).getTime();
		}
		finder.findfun1=function(){
			this.nowTimes+=(new Date()).getTime()-this.times;
			if(this.nowTimes<1000*this.sj/this.fps)
			{
				finder.findfun(this.playerMap,map,this.fo.path);
			}
		}
		finder.findfun=function(pm,mp,path){
			if(path.length<=0)
			{
				path.push({player:pm,paths:(this.getFX())})
			}
		}
		/*
		* 上1 右2 下3 左4
		*/
		finder.getFX=function(point1,point2){
			var ra:Array=new Array();
			if(point1.x<point2.x)
			{//右象限
				if(point1.y<point2.y)
				{//右下象限
					if((point2.y-point1.y)>(point2.x-point1.x))
					{
						ra.push(3);
						ra.push(1);
						ra.push(2);
						ra.push(4);
						return ra;
					}else{
						ra.push(2);
						ra.push(1);
						ra.push(3);
						ra.push(4);
						return ra;
					}
				}else{
					//右上象限
					if((point1.y-point2.y)>(point2.x-point1.x))
					{
						ra.push(1);
						ra.push(2);
						ra.push(3);
						ra.push(4);
						return ra;
					}else{
						ra.push(2);
						ra.push(1);
						ra.push(3);
						ra.push(4);
						return ra;
					}
				}
			}else{
				//左象限
				if(point1.y<point2.y)
				{//左下象限
					if((point1.x-point2.x)>(point2.y-point1.y))
					{
						ra.push(4);
						ra.push(1);
						ra.push(2);
						ra.push(3);
						return ra;
					}else{
						ra.push(3);
						ra.push(1);
						ra.push(2);
						ra.push(4);
						return ra;
					}
				}else{
					//左上象限
					if((point1.x-point2.x)>(point1.y-point2.y))
					{
						ra.push(4);
						ra.push(1);
						ra.push(2);
						ra.push(3);
						return ra;
					}else{
						ra.push(1);
						ra.push(2);
						ra.push(3);
						ra.push(4);
						return ra;
					}
				}
			}
		}
		Finder=finder;
	}
	public function resetPlayer(player:MovieClip,dj:Number){
		if(dj<=0)
		{
			say("")
		}
		var playHitMap:Object
		playHitMap=getHitTestMap(player,dj);
		playHitMap=localToGlobal(playHitMap);
		Finder.playerMap=playHitMap;
	}
	//辅助函数
	private function localToGlobal(tag:MovieClip,point:Object):Object{
		if(tag!=_root && tag!=undefined)
		{
			tag=tag._parent;
		}
		for(var i in point.top)
		{
			tag.localToGlobal(point.top[i]);
		}
		for(var i in point.bottom)
		{
			tag.localToGlobal(point.bottom[i]);
		}
		for(var i in point.left)
		{
			tag.localToGlobal(point.left[i]);
		}
		for(var i in point.right)
		{
			tag.localToGlobal(point.right[i]);
		}
		return point;
	}
	private function getHitTestMap(player:MovieClip,dj:Number):Object{
		if(player==undefined)
		{
			say("运动MC["+player+"]不正确。");
			return null;
		}
		if(dj<=0)
		{
			say("运动MC的hitTest精度设置["+dj+"]不正确");
			return null;
		}
		var o:Object=new Object();
		o.top=new Array();
		o.left=new Array();
		o.right=new Array();
		o.bottom=new Array();
		
		var l:Number=player._x;
		var r:Number=player._x+player._width;
		var t:Number=player._y;
		var b:Number=player._y+player._height;
		//----------  top curve and bottom curve
		for(var i =l;i<r;i+=dj)
		{
			var f1:Boolean=false,f2:Boolean=false;
			for(var n=t;n<b;n+=0.2)
			{
				if(player.hitTest(i,n,true))
				{
					f1=true;
					o.top.push({x:i,y:n});
					break;				}
			}
		
			for(var n=b;n>t;n-=0.2)
			{
				if(player.hitTest(i,n,true))
				{
					f1=true;
					o.bottom.push({x:i,y:n});
					break;
				}
			}
			if(!f1){ 
				o.top.push({x:l,y:t});
				o.top.push({x:r,y:t});
			}
			if(!f2){
				o.bottom.push({x:l,y:b});
				o.bottom.push({x:r,y:b});
			}
		}
		//------  left and bottom curve 
		for(var i=t;i<b;i+=dj)
		{
			var f1:Boolean=false,f2:Boolean=false;
			for(var n=l;n<r;n+=0.2)
			{
				if(player.hitTest(n,i,true))
				{
					f1=true;
					o.left.push({x:n,y:i});
					break;
				}
			}
		
			for(var n=r;n>l;n-=0.2)
			{
				if(player.hitTest(n,i,true))
				{
					f1=true;
					o.right.push({x:n,y:i});
					break;
				}
			}
			if(!f1) {
				o.left.push({x:l,y:t});
				o.left.push({x:l,y:b});
			}
			if(!f2){
				o.right.push({x:r,y:b});
				o.right.push({x,r,y:t})
			}
		}
		//-------------------------------
		return o;
	}
	private function createFinderMc(player:MovieClip):MovieClip{
		if(player==undefined)
		{
			say("运动MC["+player+"]不正确。");
			return null;
		}
		var d:Number=player.getNextHighestDepth();
		player.createEmptyMovieClip("finder"+d.toString(),d);
		var tag:MovieClip=player["finder"+d.toString()];
		tag.destroy=function(){
			this.removeMovieClip();
		}
		return tag;
	}
}